#include <iostream>
#include <fstream>
#include <random>
#include <string>

// 生成指定范围内的随机整数
int64_t getRandomNumber(int64_t min, int64_t max) {
    static std::random_device rd; // generate the seed from os
    static std::mt19937_64 eng(rd()); // generate random number according to the seed
    std::uniform_int_distribution<int64_t> dist(min, max);
    return dist(eng);
}

// 生成指定大小的文件
void generateFile(const std::string& filename, std::size_t fileSize) {
    std::ofstream file(filename, std::ios::binary);
    std::size_t bytesWritten = 0;

    constexpr std::size_t bufferSize = 4096;
    constexpr std::size_t maxNumbersPerBuffer = bufferSize / sizeof(int64_t);
    int64_t buffer[bufferSize];

    while (bytesWritten < fileSize) {
        std::size_t numbersToWrite = std::min(maxNumbersPerBuffer, (fileSize - bytesWritten) / sizeof(int64_t));
        if(numbersToWrite == 0) {
            break;
        }
        for (std::size_t i = 0; i < numbersToWrite; ++i) {
            buffer[i] = getRandomNumber(std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max());
        }
        
        file.write(reinterpret_cast<const char*>(buffer), numbersToWrite * sizeof(int64_t));
        bytesWritten += numbersToWrite * sizeof(int64_t);
    }

    file.close();
}

int main() {
    int n;
    std::cout << "Enter the value of n: ";
    std::cin >> n;

    constexpr std::size_t minFileSize = 1024; // 10KB
    constexpr std::size_t maxFileSize = 1024 * 50; // 50KB

    for (int i = 0; i < n; ++i) {
        std::string filename = std::to_string(i) + ".txt";
        std::size_t fileSize = getRandomNumber(minFileSize, maxFileSize);
        std::cout << fileSize << std::endl;
        generateFile(filename, fileSize);
    }

    std::cout << "Files generated successfully." << std::endl;

    return 0;
}